import { createRouter, createWebHistory } from 'vue-router'
import ProductManagement from '@/ProductManagement.vue' // 直接导入组件
import AfterSaleView from '@/views/AfterSaleView.vue'
import ProductView from '@/views/ProductView.vue'
import Userview from '@/views/Userview.vue'
import Useraddress from '@/components/useraddress.vue'
import Useraddressview from '@/views/Useraddressview.vue'
import App from '@/App.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'Home',
      component: ProductView // 或者保持为空，显示App.vue的默认内容
    },
    {
      path: '/admin/products',
      name: 'ProductManagement',
      component: () => import('@/ProductManagement.vue')
    },{
      path:'/aftersale',
      name:'aftersale',
      component:AfterSaleView
    },{
      path:'/product',
      name:'product',
      component:ProductView
    },{
      path:'/user',
      name:'user',
      component:Userview
    },{
      path:'/address',
      name:'address',
      component:Useraddressview
    }
  ]
})


import { useLoginStore } from '@/stores/LoginStore'
import { ElMessage } from 'element-plus'

//路由守卫 - 全局
router.beforeEach((to, from) => {
  //实例化
  let loginStore = useLoginStore()
  //判断是否需要登录
  if(to.meta.auth){
    //进行登录判断
    if(loginStore.loginToken){//说明是登录状态
      return true;//放行
    }else{
      //尚未登录
      ElMessage.error({
        message: 'sorry,请先登录',
        type: 'error'
      })
      return false;//阻止跳转
    }
  }
  return true;//当前路由中没有meta.auth，放行
})

router.afterEach((to, from) => {
  //设置页面标题
  document.title = (to.meta.title || 'hello') as string
  return true;
})


export default router
